While working with Jenkins,there was a requirement to collect details of the Jobs/builds that had been run on the Server and derive statistics like
- Total Runs of a job:
- Total Success:
- Total Failures and etc.
There could be different approaches to capture the information and the UI also shows the few trends directly,but I took the help of Python and Jenkins API to get it done.
Jenkins API
Jenkins provides REST Api which allows us to perform most of the actions and fetch the information which is usually available in the UI, programmatically.We can use the official Jenkins Builds URL and its corresponding api as an example to go through the options available.
Note: Do remember the official Jenkins server has restricted access to view build and job details from api.You should have full information available on your self hosted server though as shown below
Access token
Although the …./API/json details can be seen on the browser,you actually need configure an access token to capture them programmatically.So please go ahead and generate it using the steps already detailed on official guide
Script
Now lets come to the actual script which has been written in haste to complete the task, but it should be an useful snippet for a project to build up on.
For every URL we see in the Jenkins UI, we can append /api/json to see its JSON equivalent.In this script,we are fetching JSON data from the URL’s of the job and its executed builds.
First,we get the latest build ran and go through all its runs to get the statuses.So the complete script follows
What’s next
This script can be extended to get details of the jobs on server and their success,time to run etc and build custom stats which are not available by default in Jenkins.Also we can start managing jobs programmatically with out manually logging into the console.